package black.market.location;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class LocationDAO {
	public static final String TAG = "LocationDAO";
	private ContentResolver mContentResolver = null;
	private Context mContext = null;

	public LocationDAO(Context context) {
		super();
		this.mContext = context;
		this.mContentResolver = mContext.getContentResolver();
	}

	public boolean isLocationDatabase_Null(Uri u) {
		Log.d(TAG, "inLocationDatabase_Null(Uri " + u + ")");
		Uri uri = u;
		String[] projection = null;
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		int cnt = c.getCount();
		c.close();
		return cnt == 0;
	}

	public String getLocation_Lat() {
		String lat = null;
		Uri uri = MapContentProvider.MAP_GEOPOINT_URI;
		String[] projection = { MapContentProvider.MAP_LAT };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		if (c.moveToFirst()) {
			do {
				if (c.isLast()) {
					lat = c.getString(c.getColumnIndex(MapContentProvider.MAP_LAT));
				}
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		Log.d(TAG, "getLocation_Lat(): " + lat);
		return lat;
	}

	public String getLocation_Lng() {
		String lng = null;
		Uri uri = MapContentProvider.MAP_GEOPOINT_URI;
		String[] projection = { MapContentProvider.MAP_LNG };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		if (c.moveToFirst()) {
			do {
				if (c.isLast()) {
					lng = c.getString(c.getColumnIndex(MapContentProvider.MAP_LNG));
				}
			} while (c.moveToNext());
		}
		if (!c.isClosed()) {
			c.close();
		}
		Log.d(TAG, "getLocation_Lng(): " + lng);
		return lng;
	}

	public String[] getSimpleAddr() {
		String[] addr = null;
		Log.d(TAG, "getSimpleAddr()");
		Uri uri = MapContentProvider.MAP_ADDRESS_URI;
		String[] projection = { MapContentProvider.MAP_NAME1, MapContentProvider.MAP_NAME2, MapContentProvider.MAP_NAME3 };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		if (c.getCount() > 0) {
			addr = new String[3];
			do {
				if (c.isLast()) {
					addr[0] = c.getString(c.getColumnIndex(MapContentProvider.MAP_NAME1));
					addr[1] = c.getString(c.getColumnIndex(MapContentProvider.MAP_NAME2));
					addr[2] = c.getString(c.getColumnIndex(MapContentProvider.MAP_NAME3));
				}
			} while (c.moveToNext());
			Log.d(TAG, addr[0] + " // " + addr[1] + " // " + addr[2]);
		} else {
			Log.i(TAG, "Get SimpleAddr " + uri.toString() + " size 0");
		}
		if (!c.isClosed()) {
			c.close();
		}
		return addr;
	}
}
